﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Cocon90.Lib.Dal
{
    public abstract class DataHelper : IDataHelper
    {
        #region IDataHelper 成员
        public virtual string GetConnectionString
        {
            get { return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; }
        }
        public virtual System.Data.Common.DbCommand createCommond(string tsqlParamed, CommandType commandType, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            throw new NotImplementedException();
        }
        public virtual System.Data.Common.DbDataAdapter createDataAdapter(System.Data.Common.DbCommand selectCmd)
        {
            throw new NotImplementedException();
        }
        public virtual System.Data.Common.DbParameter createParameter(string key, object value)
        {
            throw new NotImplementedException();
        }

        public virtual int execNoQuery(string tsqlParamed, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            var cmd = createCommond(tsqlParamed, CommandType.Text, paramKeyAndValue);
            int temp = cmd.ExecuteNonQuery();
            if (cmd.Connection != null && cmd.Connection.State != ConnectionState.Closed) { cmd.Connection.Close(); cmd.Connection.Dispose(); }
            cmd.Dispose();
            return temp;
        }

        public virtual int execNoQueryProc(string storedProcedureName, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            var cmd = createCommond(storedProcedureName, CommandType.StoredProcedure, paramKeyAndValue);
            int temp = cmd.ExecuteNonQuery();
            if (cmd.Connection != null && cmd.Connection.State != ConnectionState.Closed) { cmd.Connection.Close(); cmd.Connection.Dispose(); }
            cmd.Dispose();
            return temp;
        }
        public virtual object getScalar(string tsqlParamed, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            var cmd = createCommond(tsqlParamed, CommandType.Text, paramKeyAndValue);
            object temp = cmd.ExecuteScalar();
            if (cmd.Connection != null && cmd.Connection.State != ConnectionState.Closed) { cmd.Connection.Close(); cmd.Connection.Dispose(); }
            cmd.Dispose();
            return temp;
        }

        public virtual object getScalarProc(string storedProcedureName, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            var cmd = createCommond(storedProcedureName, CommandType.StoredProcedure, paramKeyAndValue);
            object temp = cmd.ExecuteScalar();
            if (cmd.Connection != null && cmd.Connection.State != ConnectionState.Closed) { cmd.Connection.Close(); cmd.Connection.Dispose(); }
            cmd.Dispose();
            return temp;
        }

        public virtual bool getBoolean(string tsqlParamed, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            bool temp;
            bool.TryParse(getScalar(tsqlParamed, paramKeyAndValue) + "", out temp);
            return temp;
        }

        public virtual bool getBooleanProc(string storedProcedureName, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            bool temp;
            bool.TryParse(getScalarProc(storedProcedureName, paramKeyAndValue) + "", out temp);
            return temp;
        }

        public virtual System.Data.DataSet getDataSet(string tsqlParamed, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {

            DataSet ds = new DataSet();
            var cmd = createCommond(tsqlParamed, CommandType.Text, paramKeyAndValue);
            var dap = createDataAdapter(cmd);
            dap.Fill(ds);
            if (cmd.Connection != null && cmd.Connection.State != ConnectionState.Closed) { cmd.Connection.Close(); cmd.Connection.Dispose(); }
            cmd.Dispose();
            dap.Dispose();
            return ds;
        }

        public virtual System.Data.DataSet getDataSetProc(string storedProcedureName, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            DataSet ds = new DataSet();
            var cmd = createCommond(storedProcedureName, CommandType.StoredProcedure, paramKeyAndValue);
            var dap = createDataAdapter(cmd);
            dap.Fill(ds);
            if (cmd.Connection != null && cmd.Connection.State != ConnectionState.Closed) { cmd.Connection.Close(); cmd.Connection.Dispose(); }
            cmd.Dispose();
            dap.Dispose();
            return ds;
        }

        public virtual DateTime getDateTime(string tsqlParamed, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            DateTime temp = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            DateTime.TryParse(getScalar(tsqlParamed, paramKeyAndValue) + "", out temp);
            return temp;
        }

        public virtual DateTime getDateTimeProc(string storedProcedureName, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            DateTime temp = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            DateTime.TryParse(getScalarProc(storedProcedureName, paramKeyAndValue) + "", out temp);
            return temp;
        }

        public virtual float getFloat(string tsqlParamed, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            float temp = -1;
            float.TryParse(getScalar(tsqlParamed, paramKeyAndValue) + "", out temp);
            return temp;
        }

        public virtual float getFloatProc(string storedProcedureName, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            float temp = -1;
            float.TryParse(getScalarProc(storedProcedureName, paramKeyAndValue) + "", out temp);
            return temp;
        }

        public virtual int getNumber(string tsqlParamed, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            int temp = -1;
            int.TryParse(getScalar(tsqlParamed, paramKeyAndValue) + "", out temp);
            return temp;
        }

        public virtual int getNumberProc(string storedProcedureName, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            int temp = -1;
            int.TryParse(getScalarProc(storedProcedureName, paramKeyAndValue) + "", out temp);
            return temp;
        }


        public virtual string getString(string tsqlParamed, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            return getScalar(tsqlParamed, paramKeyAndValue) + "";
        }

        public virtual string getStringProc(string storedProcedureName, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            return getScalarProc(storedProcedureName, paramKeyAndValue) + "";
        }

        public virtual System.Data.DataTable getTable(string tsqlParamed, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            DataTable dt = new DataTable();
            var ds = getDataSet(tsqlParamed, paramKeyAndValue);
            if (ds.Tables != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            return dt;

        }

        public virtual System.Data.DataTable getTableProc(string storedProcedureName, params System.Data.Common.DbParameter[] paramKeyAndValue)
        {
            DataTable dt = new DataTable();
            var ds = getDataSetProc(storedProcedureName, paramKeyAndValue);
            if (ds.Tables != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            return dt;
        }

        #endregion
    }
}
